<?php 
session_start();

if (!$_SESSION['user_id'])
{
	echo "false";
	die();
}

if (!$_GET['assignment_id'])
{
	echo "-1";
	die();
}

function doubleQuoteSQL($text)
{
	return str_replace("'", "''", $text);
}

$assignment_id = $_GET['assignment_id'];

session_start();

include("class_rtf.php");
include("application/base_class.php");

$base_obj = new BaseClass();
//http://www.studysage.com.au/index.php?index.php&page=assignment_manager&view=continue_assign&id=714&tr=456
$body = mysql_fetch_array(mysql_query("select * from composedocument where id = '" . doubleQuoteSQL($assignment_id) . "'"));
 
$title = $body['assignmenttitle'];
$comments = $body['comments'];
$assainment = "<div style=\"text-align: center;\"><b>Assigment title: $title</b></div><div>" . $comments . "</div>";


$rtf = new rtf("rtf_config.php");
$rtf->setPaperSize(5);
$rtf->setPaperOrientation(1);
$rtf->setDefaultFontFace(0);
$rtf->setDefaultFontSize(20);
$rtf->setAuthor("Obby");
$rtf->setOperator("solor.motor@gmail.com");
$rtf->setTitle("RTF Document");
$rtf->addColour("#000000");
$rtf->addText($assainment);
$text = $rtf->getDocument();

$time = time();
$filename = 'temp_files_sendmail/' . $_SESSION['user_id'] . "_" . $time . ".rtf";
$fp = fopen($filename, 'w');
fwrite($fp, $text);
fclose($fp);

$id = $_SESSION['user_id'];
$qry = "select FirstName,LastName, Email from users where Id='".$id."'";
$res = mysql_query($qry);
$data = mysql_fetch_array($res);
			
$email_from = "support@studysage.com.au";					
$to = $data['Email'];
$subject="The assignment requesting";

$msg = '<p style="color:#7030A0"><b>Hi ' . $data['FirstName'] . ',</b></p>							
		<p>As you request, we send to you your assignment. Please check in your file attachment.</p>					
		<br/>
		Kind Regards,
		<br/>
		Support Team
		<br/><br/>
		http://www.studysage.com.au
		';					

$from = "Studysage <".$email_from.">";
$headers = "From: $from";
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n"; 

// preparing attachments

$message .= "--{$mime_boundary}\n";
$fp = @fopen($filename,"r");
$data = @fread($fp,filesize($filename));
@fclose($fp);
//$data = chunk_split(base64_encode($data));

$message .= "Content-Type: application/rtf; name=\"Assignment\"\n" . 
"Content-Description: Assignment\n" .
"Content-Disposition: attachment;\n" . " filename=\"Assignment.rtf\"; size=".filesize($files[$i]).";\n" . 
"Content-Transfer-Encoding: utf-8\n\n" . $data . "\n\n";
		
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email_from;

$sendMailSuccess = mail($to,$subject,$message,$headers);	
//delete file
unlink($filename);
echo "SEND";
?>